package org.jaaksi.demo.ptr.view;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import org.jaaksi.demo.ptr.R;

/**
 * Created by Fcy on 2016/3/19.<br/>
 */
public class StateView extends FrameLayout
{
	public static final byte STATE_NORMAL = 0;
	public static final byte STATE_NO_NET = 1;
	public static final byte STATE_SERVER_ERROR = 2;
	public static final byte STATE_NO_DATA = 3;
	public static final byte STATE_LOADING = 4;
	/**
	 * 上拉加载的时候发生错误，包三种no_net,no_data,SE
	 * loadmore的时候不可能出现列表没有数据，stateview一定是gone的,不做其他处理
	 */
	public static final byte STATE_LOAD_MORE_ERROR = 5;
	/**
	 * 其他状态，如ptr的refresh与加载更多，效果
	 */
	public static final byte STATE_OTHER = 6;

	private byte state;

	private View ll_state_view;
	private ImageView iv_hint;
	private TextView tv_hint, tv_hint_retry;
	private View ll_loading;
	private TextView loading_text;

	private int imgNoNetId, imgNoDataId, imgSEId;
	private int hintNoNetId, hintNoDataId, hintSEId;
	private int hintRetryNoNetId, hintRetryNoDataId, hintRetrySEId;

	public StateView(Context context)
	{
		super(context);
		initView();
	}

	public StateView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		initView();
	}

	public StateView(Context context, AttributeSet attrs, int defStyleAttr)
	{
		super(context, attrs, defStyleAttr);
		initView();
	}

	/**
	 * 初始化资源
	 */
	private void initRes()
	{
		imgNoNetId = R.drawable.img_no_net;
		imgSEId = imgNoDataId = R.drawable.img_no_data;
		hintNoNetId = R.string.str_no_net;
		hintSEId = hintNoDataId = R.string.str_server_error;
		hintRetryNoDataId = hintRetryNoNetId = hintRetrySEId = R.string.str_click_refresh;
	}

	private void initView()
	{
		View view = View.inflate(getContext(), R.layout.lib_state_view, this);
		//View view = LayoutInflater.from(getContext())
		//        .inflate(R.layout.lib_state_view, this, false);
		ll_loading = view.findViewById(R.id.ll_loading);
		loading_text = (TextView) ll_loading.findViewById(R.id.tv_content);
		// loading_text.setVisibility(GONE);

		ll_state_view = view.findViewById(R.id.ll_state_view);
		iv_hint = (ImageView) ll_state_view
		        .findViewById(R.id.iv_stateview_icon);
		tv_hint = (TextView) ll_state_view.findViewById(R.id.tv_stateview_hint);
		tv_hint_retry = (TextView) ll_state_view
		        .findViewById(R.id.tv_stateview_retry);
		//addView(view, ViewGroup.LayoutParams.MATCH_PARENT,
		//        ViewGroup.LayoutParams.MATCH_PARENT);
		initRes();
		setState(STATE_NORMAL);
	}

	public void initNoNetRes(int imgId, int hintId, int retryId)
	{
		imgNoNetId = imgId;
		hintNoNetId = hintId;
		hintRetryNoNetId = retryId;
	}

	public void initNoDataRes(int imgId, int hintId, int retryId)
	{
		imgNoDataId = imgId;
		hintNoDataId = hintId;
		hintRetryNoDataId = retryId;
	}

	public void initSERes(int imgId, int hintId, int retryId)
	{
		imgSEId = imgId;
		hintSEId = hintId;
		hintRetrySEId = retryId;
	}

	public void normal() {
		setState(STATE_NORMAL);
	}

	/**
	 * 处理没有网络和服务器异常的情况，不用于处理个数为0的
	 *
	 * @param context
	 */
	public void noDate(Context context) {
		if (!isConnectNet(context)) {
			setState(STATE_NO_NET);
		} else {
			setState(STATE_SERVER_ERROR);
		}
	}

	public int getState()
	{
		return state;
	}

	public void setState(byte state)
	{
		this.state = state;
		switch (state)
		{
			case STATE_NO_NET:
				showStateView(imgNoNetId, hintNoNetId, hintRetryNoNetId);
				break;
			case STATE_NO_DATA:
				showStateView(imgNoDataId, hintNoDataId, hintRetryNoDataId);
				break;
			case STATE_SERVER_ERROR:
				showStateView(imgSEId, hintSEId, hintRetrySEId);
				break;
			// case STATE_LOADING:
			// showStateView(-1, -1, -1);
			// break;
			default:
				showStateView(-1, -1, -1);
				break;
		}
	}

	private void showStateView(int imgId, int hintId, int hintRetryId)
	{
		if (state == STATE_LOAD_MORE_ERROR)
		{
			return;
		}

		if (state == STATE_NORMAL)
		{
			ll_state_view.setVisibility(GONE);
			ll_loading.setVisibility(GONE);
		}
		else if (state == STATE_OTHER)
		{
			// ll_state_view.setVisibility(GONE);
			ll_loading.setVisibility(GONE);
		}
		else if (state == STATE_LOADING)
		{
			// ll_state_view.setVisibility(GONE);
			ll_loading.setVisibility(VISIBLE);
		}
		else
		{
			ll_loading.setVisibility(GONE);
			ll_state_view.setVisibility(VISIBLE);
			if (imgId != -1)
			{
				iv_hint.setImageResource(imgId);
				iv_hint.setVisibility(VISIBLE);
			}
			else
			{
				iv_hint.setVisibility(GONE);
			}
			if (hintId == -1)
			{
				tv_hint.setVisibility(GONE);
			}
			else
			{
				tv_hint.setText(hintId);
				tv_hint.setVisibility(VISIBLE);
			}
			if (hintRetryId == -1)
			{
				tv_hint_retry.setVisibility(GONE);
			}
			else
			{
				tv_hint_retry.setText(hintRetryId);
				tv_hint_retry.setVisibility(VISIBLE);
			}
		}

	}

	private boolean isConnectNet(Context context) {
		ConnectivityManager conManager =
				(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (conManager == null) {
			return false;
		} else {
			NetworkInfo[] info = conManager.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].isConnected()) {
						return true;
					}
				}
			}
		}
		return false;
	}

	public void setOnRetryListener(OnClickListener onRetryListener)
	{
		ll_state_view.setOnClickListener(onRetryListener);
	}
}
